##################################################
#
# TABLES OF REPRODUCTIVE BEHAVIOR MODELS FOR
# OIL, ISLAM, and WOMEN PROJECT
#
# Paul Musgrave and Yu-Ming Liou
# musgrave@umass.edu and yl254@georgetown.edu
#
# Created 27 February 2016
#
##################################################


##################################################
# R Housekeeping
##################################################

# Remove and empty workspace
rm(list=ls())

#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Packages
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 

library(foreign)
library(sandwich)
library(apsrtable)
library(stargazer)
library(tonymisc)  # includes summaryR()
library(MASS) # for polr() which runs our ologit

#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Functions
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 

mywd <- "/Users/paulmusgrave/Dropbox/0001 Academic Projects/Ongoing/0127 Oil Islam Women/ISQ Accepted Submission/Replication"

setwd(paste(mywd,"/Code",sep=""))

source("statifyFN.R")
source("multipleOLSFN.R")
source("genModelsFN.R")
source("plotlineFN.R")
source("plotprepFN.R")
source("modelDefinitions.R")
source("tableOLSFN.R")
source("prepper.R")
source("helperFN.R")
tablenames <- read.csv("allmodels.csv", stringsAsFactors=FALSE)

p142.09.names <- prepper(p142.09)
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 
# Load Data
#  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  # 

setwd(paste(mywd,"/Data",sep=""))

data <- read.dta("NewDVModelsXSforISQFINAL2015.dta")
data$logGDPcap6372_sq <- data$logGDPcap6372 * data$logGDPcap6372

setwd(paste(mywd,"/Drafts/Tables",sep=""))


#################################################
#
# Models of Oil Income and Vice Issues
# 
#################################################

# Total Fertility 1

tab.totfert.1 <- tableOLS(formulas=genModels(c("totfert"),
                                              c(p140alt)),
                           modelnames=c(names(p140base),names(p142base)))

stargazer(tab.totfert.1,
          title= "Models of Oil Income Per Capita and Total Fertility",
          dep.var.labels = c("Fertility Rate"),
          covariate.labels=c(p140alt.names[,2],"Intercept"),
          out="ISQFINALAppendixTable13.tex",
          style="apsr",
          label="tab:totfert1",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# Total Fertility 2
tab.totfert.2 <- tableOLS(formulas=genModels(c("totfert"),
                                             c(p142base)),
                          modelnames=c(names(p142base)))
stargazer(tab.totfert.2,
          title= "Models of Oil Income Per Capita and Total Fertility",
          dep.var.labels = c("Fertility Rate"),
          covariate.labels=c(p142base.names[,2],"Intercept"),
          out="ISQFINALAppendixTable14.tex",
          style="apsr",
          label="tab:totfert2",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# Adolescent Fertility 1
tab.adfert.1 <- tableOLS(formulas=genModels(c("adfert"),
                                            c(p140alt)),
                         modelnames=c(names(p140base),names(p142base)))

stargazer(tab.adfert.1,
          title= "Models of Oil Income Per Capita and Adolescent Fertility",
          dep.var.labels = c("Fertility Rate"),
          covariate.labels=c(p140alt.names[,2],"Intercept"),
          out="ISQFINALAppendixTable15.tex",
          style="apsr",
          label="tab:adfert1",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# Adolescent Fertility 2
tab.adfert.2 <- tableOLS(formulas=genModels(c("adfert"),
                                            c(p142base)),
                         modelnames=c(names(p142base)))
stargazer(tab.adfert.2,
          title= "Models of Oil Income Per Capita and Adolescent Fertility",
          dep.var.labels = c("Fertility Rate"),
          covariate.labels=c(p142base.names[,2],"Intercept"),
          out="ISQFINALAppendixTable16.tex",
          style="apsr",
          label="tab:adfert2",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# Contraception 1
# Contraception 1

tab.contraception.1 <- tableOLS(formulas=genModels(c("avgcontraception"),
                                                   c(p140alt)),
                                modelnames=c(names(p140base),names(p142base)))

stargazer(tab.contraception.1,
          title= "Models of Oil Income Per Capita and Contraception",
          dep.var.labels = c("Fertility Rate"),
          covariate.labels=c(p140alt.names[,2],"Intercept"),
          out="ISQFINALAppendixTable17.tex",
          style="apsr",
          label="tab:contraception1",
          no.space=TRUE,
          omit.stat=c("ser","f"))

# Contraception 2
tab.contraception.2 <- tableOLS(formulas=genModels(c("avgcontraception"),
                                                   c(p142base)),
                                modelnames=c(names(p142base)))
stargazer(tab.contraception.2,
          title= "Models of Oil Income Per Capita and Contraception",
          dep.var.labels = c("Fertility Rate"),
          covariate.labels=c(p142base.names[,2],"Intercept"),
          out="ISQFINALAppendixTable18.tex",
          style="apsr",
          label="tab:contraception2",
          no.space=TRUE,
          omit.stat=c("ser","f"))
